﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;


namespace Drifting.Graphics.PhysicalObjects.Vehicles
{
    public class Atlantis : PhysicalObject
    {
        public Atlantis(Game1 game)
        {
            Body = game.Content.Load<Model>("models\\SpaceShuttle");
            Body_AbsoluteMatrix = new Matrix[Body.Bones.Count];
            Body.CopyAbsoluteBoneTransformsTo(Body_AbsoluteMatrix);

            body_scale = 0.023855f;    // 0.023855f is 1:1
            m_calibration = Matrix.CreateRotationY((float)Math.PI/2) * Matrix.CreateTranslation(0, 0.5f, 0);
        }

        public double Scale
        {
            get { return body_scale; }
            set
            {
                if (value < 0 || value > 1)
                    throw new ArgumentException("Scale must be in [0, 1]");

                body_scale *= (float)value;
                m_calibration = Matrix.CreateRotationY((float)Math.PI / 2) * Matrix.CreateTranslation(0, (float)(0.5f * value), 0);
            }
        }

        public void Draw(Matrix camera, Matrix projection, double x, double y, double z, double Ax, double Ay, double Az)
        {
            Vector3 axis = new Vector3((float)Ax,(float)Ay,(float)Az);
            float angle = axis.Length();
            axis.Normalize();

            Matrix config = Matrix.Identity;
            if (angle != 0)               
                Matrix.CreateFromAxisAngle(ref axis, angle, out config);
            config.Translation = new Vector3((float)x, (float)y, (float)z);

            foreach (ModelMesh mesh in Body.Meshes)
            {
                foreach (BasicEffect effect in mesh.Effects)
                {
                    effect.EnableDefaultLighting();
                    effect.View = camera;
                    effect.Projection = projection;
                    effect.World =

                        Body_AbsoluteMatrix[mesh.ParentBone.Index] *

                        Matrix.CreateScale(body_scale) *

                        m_calibration *

                        config;
                }
                mesh.Draw();
            }
        }

        public void Draw(Matrix camera, Matrix projection, double x, double y, double z, double q0, double q1, double q2, double q3)
        {
            Matrix config = Matrix.CreateFromQuaternion(new Quaternion((float) q0, (float)q1, (float)q2, (float)q3));
            config.Translation = new Vector3((float)x, (float)y, (float)z);

            foreach (ModelMesh mesh in Body.Meshes)
            {
                foreach (BasicEffect effect in mesh.Effects)
                {
                    effect.EnableDefaultLighting();
                    effect.View = camera;
                    effect.Projection = projection;
                    effect.World =

                        Body_AbsoluteMatrix[mesh.ParentBone.Index] *

                        Matrix.CreateScale(body_scale) *

                        m_calibration *

                        config;
                }
                mesh.Draw();
            }
        }
    }
}
